package cn.com.inossem.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.inossem.activity.ImageViewDialog;
import cn.com.inossem.activity.MainActivity;
import cn.com.inossem.activity.R;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.model.BaGuaListInfoBean;
import cn.com.inossem.utils.AsyncImageLoader;
import cn.com.inossem.utils.AsyncImageLoader.ImageCallback;
import cn.com.inossem.utils.Utils;

public class BaGuaAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private List<BaGuaListInfoBean> messageInfo;
	private Handler handler;
	private boolean isShow;

	public BaGuaAdapter(Context mContext, List<BaGuaListInfoBean> messageInfo,
			Handler handler, boolean isShow) {
		super();
		this.mContext = mContext;
		this.messageInfo = messageInfo;
		this.handler = handler;
		this.isShow = isShow;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		// return messageInfo.size();
		return messageInfo.size();
	}

	@Override
	public BaGuaListInfoBean getItem(int arg0) {
		// return messageInfo.get(arg0);
		return messageInfo.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.bagua_item_layout, null);
			holder.nextlayout = (LinearLayout) convertView
					.findViewById(R.id.nextlayout);
			// holder.llayout = (LinearLayout) convertView
			// .findViewById(R.id.llayout);
			holder.gridView = (GridView) convertView
					.findViewById(R.id.gridview);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.text = (TextView) convertView.findViewById(R.id.text);
			holder.praseCount = (TextView) convertView
					.findViewById(R.id.praseCount);
			holder.dianzanlayout = (LinearLayout) convertView
					.findViewById(R.id.dianzanlayout);
			holder.textlength = (TextView) convertView
					.findViewById(R.id.textlength);
			holder.pic = (ImageView) convertView.findViewById(R.id.pic);
			holder.dianzan = (ImageView) convertView.findViewById(R.id.dianzan);
			// holder.gridView = (GridView) convertView
			// .findViewById(R.id.gridview);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// holder.llayout.removeAllViews();
		// addView(holder.llayout, messageInfo.get(position).getPicList());
		// if (!messageInfo.get(position).getPicList().equals(""))
		// holder.gridView.setAdapter(new ImageAdapter(mContext, messageInfo
		// .get(position).getPicList(), holder.gridView));

		if (!messageInfo.get(position).getPicList().equals("")) {
			holder.gridView.setVisibility(View.VISIBLE);
			String[] pic = { "" };
			if (messageInfo.get(position).getPicList().contains(",")) {
				pic = messageInfo.get(position).getPicList().split(",");
			} else {
				pic[0] = messageInfo.get(position).getPicList();
			}
			int dipheight = 0;
			if (pic.length < 4) {
				dipheight = 90;
			} else if (pic.length < 7) {
				dipheight = 180;
			} else {
				dipheight = 270;
			}
			LinearLayout.LayoutParams params = (android.widget.LinearLayout.LayoutParams) holder.gridView
					.getLayoutParams();
			params.height = Utils.dip2px(mContext, dipheight);
			holder.gridView.setLayoutParams(params);

			holder.gridView.setAdapter(new ImageAdapter(mContext,
					holder.gridView, messageInfo.get(position).getPicList()));
		} else {
			holder.gridView.setVisibility(View.GONE);
		}
		holder.nextlayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (Utils.isDenglu == 0) {
					((MainActivity) mContext).setPageView(3);
				} else {
					Message me = new Message();
					me.what = 2;
					Bundle bun = new Bundle();
					bun.putInt("position", position);
					me.setData(bun);
					handler.sendMessage(me);
				}
			}
		});
		holder.name.setText(getItem(position).getName());
		if (isShow) {
			holder.master = (TextView) convertView.findViewById(R.id.master);
			if (Utils.messageInfo.userInfo.getId() == getItem(position).getUserID()) {
				holder.master.setVisibility(View.VISIBLE);
			} else {
				holder.master.setVisibility(View.GONE);
			}
		}
		holder.text.setText(getItem(position).getText());
		holder.praseCount.setText(getItem(position).getPraseCount());
		holder.textlength.setText(getItem(position).getChildren());

		if (!getItem(position).getIcon().equals("")) {
			holder.pic.setBackgroundResource(getResource(getItem(position)
					.getIcon()));
		}
		if (getItem(position).getIsMePraise().equals("0")) {
			holder.dianzan.setBackgroundResource(R.drawable.dianzan);
		} else {
			holder.dianzan.setBackgroundResource(R.drawable.dianzan1);
		}
		holder.dianzanlayout.setTag(holder);
		holder.dianzanlayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				ViewHolder hold = (ViewHolder) arg0.getTag();
				if (Utils.isDenglu == 0) {
					Toast.makeText(mContext, "请登录！", Toast.LENGTH_LONG).show();
				} else if (getItem(position).getIsMePraise().equals("0")) {
					System.out.println("getItem(position).getIsMePraise():"
							+ getItem(position).getIsMePraise() + "111");
					getItem(position).setIsMePraise("1");
					getItem(position)
							.setPraseCount(
									(Integer.parseInt(getItem(position)
											.getPraseCount()) + 1) + "");
					Toast.makeText(mContext, "点赞成功！", Toast.LENGTH_LONG).show();
					hold.dianzan.setBackgroundResource(R.drawable.dianzan1);
					Message me = new Message();
					me.what = 1;
					Bundle bun = new Bundle();
					bun.putString("id", getItem(position).getId());
					me.setData(bun);
					handler.sendMessage(me);
				} else {
					System.out.println("getItem(position).getIsMePraise():"
							+ getItem(position).getIsMePraise() + "222");
					getItem(position).setIsMePraise("0");
					getItem(position)
							.setPraseCount(
									(Integer.parseInt(getItem(position)
											.getPraseCount()) - 1) + "");

					Toast.makeText(mContext, "取消点赞！", Toast.LENGTH_LONG).show();
					hold.dianzan.setBackgroundResource(R.drawable.dianzan1);
					Message me = new Message();
					me.what = 1;
					Bundle bun = new Bundle();
					bun.putString("id", getItem(position).getId());
					me.setData(bun);
					handler.sendMessage(me);
				}
			}
		});

		return convertView;
	}

	private void addView(LinearLayout llayout, String picList) {
		if (picList == null) {
			return;
		}
		if (picList.equals("")) {
			return;
		}
		String[] pic = { "" };
		if (picList.contains(",")) {
			pic = picList.split(",");
		} else {
			pic[0] = picList;
		}
		LinearLayout llayoutItem = null;
		for (int i = 0; i < pic.length; i++) {
			if (i % 3 == 0) {
				llayoutItem = new LinearLayout(mContext);
				llayoutItem.setLayoutParams(new LayoutParams(
						LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
				llayoutItem.setOrientation(LinearLayout.HORIZONTAL);
				llayoutItem.setGravity(Gravity.CENTER_HORIZONTAL);
				llayout.addView(llayoutItem);
			}
			final ImageView imageView = new ImageView(mContext);
			imageView.setPadding(5, 0, 5, 5);
			imageView.setLayoutParams(new LayoutParams(Utils.dip2px(mContext,
					80), Utils.dip2px(mContext, 80)));
			imageView.setTag(WSConfig.IMAGE_URL + pic[i]);

			imageView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(mContext, ImageViewDialog.class);
					intent.putExtra("path", arg0.getTag().toString());
					mContext.startActivity(intent);

				}
			});

			// imageView.setImageResource(R.drawable.pic08);

			AsyncImageLoader asyncImageLoader = new AsyncImageLoader(mContext);
			Drawable drawable = asyncImageLoader.loadDrawable(
					WSConfig.IMAGE_URL + pic[i], new ImageCallback() {
						@Override
						public void imageLoaded(Drawable imageDrawable,
								String imageUrl) {
							if (imageDrawable != null) {
								imageView.setBackgroundDrawable(imageDrawable);
							}
						}
					});
			if (drawable != null) {
				imageView.setBackgroundDrawable(drawable);
			}
			llayoutItem.addView(imageView);
		}

	}

	/**
	 * 获取图片名称获取图片的资源id的方法
	 * 
	 * @param imageName
	 * @return
	 */
	public int getResource(String imageName) {
		int resId = mContext.getResources().getIdentifier(imageName,
				"drawable", mContext.getPackageName());
		return resId;
	}

	public final class ViewHolder {
		public ImageView pic;
		public LinearLayout llayout;
		public LinearLayout nextlayout;
		public TextView name;
		public TextView master;
		public TextView text;
		public TextView praseCount;
		public TextView textlength;
		public LinearLayout dianzanlayout;
		public ImageView dianzan;
		public GridView gridView;
	}
}
